﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using System.Security.Cryptography;

namespace Utilities
{
    public class Utility
    {
        public static void BindRadComboBox(RadComboBox theRadComboBox, DataSet dataSource, string dataTextField, string dataValueField, bool hasEmptyItem)
        {
            try
            {
                if (theRadComboBox != null && dataSource != null)
                {
                    theRadComboBox.DataTextField = dataTextField;
                    theRadComboBox.DataValueField = dataValueField;

                    theRadComboBox.DataSource = dataSource;
                    theRadComboBox.DataBind();

                    if (hasEmptyItem == true && theRadComboBox.Items.Count > 0)
                    {
                        RadComboBoxItem item = new RadComboBoxItem();
                        item.Text = string.Empty;
                        item.Value = string.Empty;
                        item.Selected = true;

                        theRadComboBox.Items.Insert(0, item);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void BindCheckBoxList(CheckBoxList theCheckBoxList, DataSet dataSource, string dataTextField, string dataValueField)
        {
            try
            {
                if (theCheckBoxList != null && dataSource != null)
                {
                    theCheckBoxList.DataTextField = dataTextField;
                    theCheckBoxList.DataValueField = dataValueField;

                    theCheckBoxList.DataSource = dataSource;
                    theCheckBoxList.DataBind();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string Parser(string message)
        {
            string strResult = string.Empty;

            strResult = message.Replace("\n", "\\n");
            strResult = strResult.Replace("\r" + "\\n", "\\n");
            strResult = strResult.Replace("'", "\\'");

            return strResult;
        }

        public static void ShowMessageBox(System.Web.UI.Page page, string message)
        {
            string strScript = "<script type='text/javascript'>";
            strScript += "alert('" + Parser(message) + "');";
            strScript += "</script>";

            if (!page.ClientScript.IsStartupScriptRegistered("clientScript"))
            {
                page.ClientScript.RegisterStartupScript(page.GetType(), "clientScript", strScript);
            }
        }

        public static void TranslateGridPagerItem(Telerik.Web.UI.GridItemEventArgs e)
        {
            try
            {
                if (e.Item is GridPagerItem)
                {
                    ((Label)e.Item.FindControl("GoToPageLabel")).Text = "Chuyển trang ";
                    ((Button)e.Item.FindControl("GoToPageLinkButton")).Text = "Chuyển";
                    ((Button)e.Item.FindControl("GoToPageLinkButton")).Width = Unit.Pixel(55);
                    ((RadNumericTextBox)e.Item.FindControl("ChangePageSizeTextBox")).Width = Unit.Pixel(20);
                    ((RadNumericTextBox)e.Item.FindControl("GoToPageTextBox")).Width = Unit.Pixel(20);
                    ((Label)e.Item.FindControl("ChangePageSizeLabel")).Text = "Số dòng hiển thị: ";
                    ((Button)e.Item.FindControl("ChangePageSizeLinkButton")).Text = "Thay đổi";
                    ((Button)e.Item.FindControl("ChangePageSizeLinkButton")).Width = Unit.Pixel(60);
                    Label PageOfLabel = (Label)e.Item.FindControl("PageOfLabel");
                    PageOfLabel.Text = PageOfLabel.Text.Replace("of", "/");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string HashMD5(string text)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(text);
            bs = md5Hasher.ComputeHash(bs);

            StringBuilder s = new StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            return s.ToString();
        }

        public static string FormatNumber(string value, string formatStyle, TypeCode type)
        {
            string result = value;

            if (!string.IsNullOrEmpty(value))
            {
                if (type == TypeCode.Int64)
                {
                    long longNumber;

                    if (long.TryParse(value, out longNumber))
                    {
                        result = longNumber.ToString(formatStyle);
                    }
                }

                if (type == TypeCode.Decimal)
                {
                    decimal decimalNumber;

                    if (decimal.TryParse(value, out decimalNumber))
                    {
                        result = decimalNumber.ToString(formatStyle);
                    }
                }
            }

            return result;
        }
    }
}
